package com.frostmourne.loreoriginex.objects;

import com.frostmourne.loreoriginex.LoreOriginEX;
import com.frostmourne.loreoriginex.api.events.AttributeUpdateEvent;
import com.frostmourne.loreoriginex.utils.TaskUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EntityData {

    private List<LoreOriginData> loreOriginData;
    private LivingEntity livingEntity;

    public List<LoreOriginData> getLoreOriginData() {
        return loreOriginData;
    }

    public void setLoreOriginData(List<LoreOriginData> loreOriginData) {
        this.loreOriginData = loreOriginData;
    }


    public EntityData(LivingEntity entity) {
        this.livingEntity = entity;
        this.loreOriginData = new ArrayList<>();
        for (ItemStack item : entity.getEquipment().getArmorContents()){
            if (item == null || !item.hasItemMeta() || !item.getItemMeta().hasLore()){
                continue;
            }
            List<LoreOriginData> data = LoreOriginEX.getOriginManager().getAttributeByLore(item.getItemMeta().getLore());
            if (data.size() == 0){
                continue;
            }
            this.loreOriginData.addAll(data);
        }
    }

    public void tryUpdate(){
        Event event = new AttributeUpdateEvent(livingEntity,this.loreOriginData);
        Bukkit.getPluginManager().callEvent(event);
        TaskUtils.refreshRuntimeAttr(livingEntity);
    }

    public LoreOriginData getLoreOriginDataByKeyName(String key){
        for (LoreOriginData data : this.loreOriginData){
            if (data.getLoreOrigin().getName().equalsIgnoreCase(key)){
                return data;
            }
        }
        return null;
    }

}
